﻿/*
 * 登记新的病人信息，编辑现有的病人信息
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WH.NurseSys.Model;
using System.Configuration;
using WH.NurseSys.BLL;

namespace WH.NurseSys.Report
{
    public partial class EditPatientForm : Form
    {
        Patient patient;
        bool newPatient = true; //是否新建病人档案

        public EditPatientForm()
        {
            InitializeComponent();
        }

        public EditPatientForm(Patient patient):this()
        {
            this.patient = patient;
            newPatient = false;
        }

        private void EditPatientForm_Load(object sender, EventArgs e)
        {
            if (this.patient != null)
            {
                textBoxName.Text = patient.Name;
                if (patient.Gender)
                    radioButtonMale.Checked = true;
                else
                    radioButtonFemale.Checked = true;
                textBoxAge.Text = patient.Age.ToString();
                if (patient.AgeUnit == "D")
                    comboBoxAgeUnit.SelectedIndex = 1;
                else
                    comboBoxAgeUnit.SelectedIndex = 0;
                textBoxInpatientNo.Text = patient.InpatientNo;
                textBoxBedNo.Text = patient.BedNo;
                textBoxDoctor.Text = patient.Doctor;
                textBoxClinicDiag.Text = patient.ClinicDiag;
                checkBoxInHosp.Checked = patient.InHosp;
            }
            else
            {
                radioButtonMale.Checked = true;
                comboBoxAgeUnit.SelectedIndex = 0;
            }
            if (ConfigurationManager.AppSettings["rank"] == "3")
            {
                if (Global.deptList == null)
                {
                    toolStripProgressBar.Style = ProgressBarStyle.Marquee;
                    this.backgroundWorkerDept.RunWorkerAsync();
                }
                else
                {
                    comboBoxDept.DataSource = Global.deptList;
                    comboBoxDept.DisplayMember = "Name";
                    comboBoxDept.ValueMember = "Id";
                }
            }
            else
            {
                List<Dept> deptList = new List<Dept>();
                Dept dept = new Dept { Id = new Guid(ConfigurationManager.AppSettings["deptId"]), Name = ConfigurationManager.AppSettings["deptName"] };
                deptList.Add(dept);
                comboBoxDept.DataSource = deptList;
                comboBoxDept.DisplayMember = "Name";
                comboBoxDept.ValueMember = "Id";
            }
        }

        //不处理，退出
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        //保存病人信息，insert or update
        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.buttonOK.Enabled = false;
            //录入新病人
            if (newPatient)
            {
                patient = new Patient();
                patient.Id = Guid.NewGuid();
                if (!validate(patient))
                    return;
                if (comboBoxAgeUnit.SelectedIndex == 0)
                    patient.AgeUnit = "Y";
                else
                    patient.AgeUnit = "D";
                patient.BedNo = textBoxBedNo.Text;
                patient.ClinicDiag = textBoxClinicDiag.Text;
                patient.DeptId = (Guid)comboBoxDept.SelectedValue;
                patient.Doctor = textBoxDoctor.Text;
                if (radioButtonMale.Checked)
                    patient.Gender = true;
                else
                    patient.Gender = false;
                patient.InDate = DateTime.Now;
                patient.InHosp = checkBoxInHosp.Checked;
                patient.InpatientNo = textBoxInpatientNo.Text;
                patient.Name = textBoxName.Text;
                patient.OutDate = DateTime.Now;
            }
            //编辑现有病人
            else
            {                
                if (!validate(patient))
                    return;
                if (comboBoxAgeUnit.SelectedIndex == 0)
                    patient.AgeUnit = "Y";
                else
                    patient.AgeUnit = "D";
                patient.BedNo = textBoxBedNo.Text;
                patient.ClinicDiag = textBoxClinicDiag.Text;
                patient.DeptId = (Guid)comboBoxDept.SelectedValue;
                patient.Doctor = textBoxDoctor.Text;
                if (radioButtonMale.Checked)
                    patient.Gender = true;
                else
                    patient.Gender = false;              
                patient.InHosp = checkBoxInHosp.Checked;
                patient.InpatientNo = textBoxInpatientNo.Text;
                patient.Name = textBoxName.Text;              
            }
            toolStripProgressBar.Style = ProgressBarStyle.Marquee;
            toolStripStatusLabelMsg.Text = "正在保存信息";
            this.backgroundWorkerPatient.RunWorkerAsync(patient);
        }

        //效验输入有效性
        bool validate(Patient patient)
        {
            //textBoxAge
            if (string.IsNullOrEmpty(textBoxAge.Text))
            {
                textBoxAge.BackColor = Color.MediumSeaGreen;
                return false;
            }
            else
            {
                int age;
                if (int.TryParse(textBoxAge.Text, out age))
                {
                    textBoxAge.BackColor = Color.White;
                    patient.Age = age;
                    return true;
                }
                else
                {
                    textBoxAge.BackColor = Color.MediumSeaGreen;
                    return false;
                }
            }
        }

        //异步获取科室列表
        private void backgroundWorkerDept_DoWork(object sender, DoWorkEventArgs e)
        {
            DeptBLL deptBLL = new DeptBLL();
            List<Dept> deptList = deptBLL.GetList();
            e.Result = deptList;
        }

        //异步获取科室列表完成
        private void backgroundWorkerDept_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripProgressBar.Style = ProgressBarStyle.Continuous;
            List<Dept> deptList = e.Result as List<Dept>;
            Global.deptList = deptList;
            comboBoxDept.DataSource = deptList;
            comboBoxDept.DisplayMember = "Name";
            comboBoxDept.ValueMember = "Id";
        }

        //异步保存病人信息
        private void backgroundWorkerPatient_DoWork(object sender, DoWorkEventArgs e)
        {
            PatientBLL patientBLL = new PatientBLL();
            Patient patient = e.Argument as Patient;
            if (newPatient)
            {
                e.Result = patientBLL.Insert(patient);
            }
            else
            {
                e.Result = patientBLL.Update(patient);
            }
        }

        //异步保存病人信息完成
        private void backgroundWorkerPatient_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripProgressBar.Style = ProgressBarStyle.Continuous;
            
            if (Convert.ToBoolean(e.Result))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("保存信息失败");
                toolStripStatusLabelMsg.Text = "保存信息失败";
            }   
        }
       
    }
}
